package some

import (
	"encoding/json"
	"io"
	"net/http"

	"gitee.com/thubcc/p2pgit/classedpack"
	"gitee.com/thubcc/p2pgit/types"
)

// HiterFromServer get all api from server
func HiterFromServer(url string) (gs types.SomeHiter, err error) {
	var resp *http.Response
	resp, err = http.Get(url + "/api")
	if err != nil {
		return
	}
	defer resp.Body.Close()
	dec := json.NewDecoder(resp.Body)
	var apis []string
	err = dec.Decode(&apis)
	if err != nil && err != io.EOF {
		return
	}
	for _, api := range apis {
		if classedpack.CheckAPIName(api) == nil {
			var cpc types.Geter
			cpc, err = classedpack.NewBasicAPI(url, api)
			if err != nil {
				return
			}
			gs = append(gs, cpc)
		}
	}
	gs, err = MergeHiters(gs)
	return
}
